<template>
<div>
  <!--头部面包屑-->
  <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>权限管理</el-breadcrumb-item>
  <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
     <!--头部按钮-->
     <el-card>
     <el-row>
      <el-button type="primary">添加用户</el-button>
     </el-row>
  <!--列表区域-->
    <el-table :data="rolesList" style="width: 100%" stripe border >
     <el-table-column  type="expand">
     <template slot-scope="scope">
     <!--一级-->
     <el-row v-for="(items1,i) in scope.row.children" :key="items1.id" :class='["bubom","vatch", i===0?"buttn":""]'>
     <el-col :span="5" @click="scope">
     <el-tag closable @close='romove(scope.row, items1.id)'>
     {{items1.authName}}
     </el-tag>
     <i class='el-icon-caret-right'></i>
     </el-col>
     <!--二级-->
     <el-col :span="19">
     <el-row v-for="(item2,i2) in items1.children" :key="item2.id" :class='["bubom", i2===0?"buttn":""]'>
     <el-col :span="5">
     <el-tag type="success" closable @close='romove(scope.row,item2.id)'>{{item2.authName}}</el-tag>
     <i class='el-icon-caret-right'></i>
     </el-col>
     <!--三级-->
     <el-col :span="19">
     <el-tag type="warning" v-for="item3 in item2.children" :key="item3.id" closable @close='romove(scope.row,item3.id)'>{{item3.authName}}</el-tag>
     <i class='el-icon-caret-right'></i>
     </el-col>
     </el-row>
     </el-col>
     </el-row>
     </template>

     </el-table-column>
    <el-table-column prop="roleName" label="角色名称"></el-table-column>
    <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
    <el-table-column  label="操作">
    <template slot-scope="scope">
       <!--修改用户信息-->
      <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
      <!-- 删除用户-->
      <el-button type="danger" icon="el-icon-delete" size="mini" >删除</el-button>
      <!--操作角色-->
      <el-button type="warning" icon='el-icon-setting' size="mini" @click="getroleName(scope.row)">分配角色</el-button>
      </template>
    </el-table-column>
    </el-table>
     </el-card>
     <!--分配角色弹出框-->
     <el-dialog title="提示" :visible.sync="dialogVisible" width="50%" @close='getrefresh'>
     <!--主体内容-->
  <el-tree :data="roleName"
  :props="getNamee"
  show-checkbox
  default-expand-all
  node-key='id'
  :default-checked-keys='defKey'
  ></el-tree>
  <!--底部按钮-->
     <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
</el-dialog>
    </div>
</template>
<script>
export default {
  data () {
    return {
      rolesList: [],
      dialogVisible: false,
      // 树形数组
      roleName: [],
      // 树形对象
      getNamee: {
        children: 'children',
        label: 'authName'
      },
      // 树形勾选数组
      defKey: []
    }
  },
  created () {
    this.rolesUser()
  },
  methods: {
    async rolesUser () {
      let{ data: res } = await this.axios.get('roles')
      console.log(res)
      if (res.meta.status !== 200) return this.$messages.error('获取失败')
      this.rolesList = res.data
    },
    // 删除标签方法
    async romove (roleid, rowid) {
      let res = await this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (res === 'cancel') {
        return this.$message.info('取消删除')
      }
      let { data: ros } = await this.axios.delete(`roles/${roleid.id}/rights/${rowid}`)
      if (ros.meta.status !== 200) return this.$message.error('删除失败')
      //   console.log(res)
      roleid.children = ros.data
    },
    // 树形角色图方法
    async getroleName (rolesy) {
      this.dialogVisible = true
      let { data: res } = await this.axios.get('rights/tree')
      // console.log(res)
      this.roleName = res.data
      // 启动递归
      this.getRecursion(rolesy, this.defKey)
    },
    // 树形勾选递归方法
    getRecursion (node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getRecursion(item, arr)
      })
    },
    // 清空树形勾选
    getrefresh () {
      this.defKey = []
    }
  }
}
</script>
<style lang="less" scoped>
.el-card{
  margin-top:15px;
}
.el-table{
  margin-top:10px;
}
.el-tag{
    margin: 10px;
}
.buttn{
    border-top: 1px solid #eee
}
.bubom{
    border-bottom:1px solid #eee
}
.vatch{
    display: flex;
    align-items: center;
}
</style>
